package com.winz.cultureapp.wxapi;

import android.content.Intent;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.winz.cultureapp.R;
import com.winz.cultureapp.application.DemoApplication;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.PayResultBean;

import org.greenrobot.eventbus.EventBus;

public class WXPayEntryActivity extends BaseAppCompatActivity implements IWXAPIEventHandler{

	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

	private IWXAPI api;

	@Override
	protected int contentView() {
		return R.layout.pay_result;
	}

	@Override
	protected void initViews() {
		api = WXAPIFactory.createWXAPI(this, Constants.APP_INFO.WX.AppID);
		api.handleIntent(getIntent(), this);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) { // 支付成功
			EventBus.getDefault().post(new PayResultBean(DemoApplication.cur_source,resp.errCode));
			switch (resp.errCode){
				case 0:
					showMsg("支付成功");
					break;
				case -1:
					showMsg("支付失败");
					DemoApplication.cur_money = 0;
					break;
				case -2:
					showMsg("取消支付");
					DemoApplication.cur_money = 0;
					break;
			}

			finish();
		}else {
			showMsg("支付失败");
			finish();
		}
	}
}